<?php
/**
 * update associated profiles
 */
function profDAUpd($mod = ""){
	global $objSDAO;
	$errMsg = "";
	$dataArea = sxREQUEST('da');
	$sSQL = "DELETE FROM data_area_permission " .						
				"WHERE data_area='".addslashes(sxGPC($dataArea))."'";
	$objSDAO->exec($sSQL);
	foreach($_REQUEST as $key=>$value){
		if(strpos($key,"id_") !== false){					
			$idProf = $value;
			$permission = (sxREQ("perm_$idProf"));
			if($permission != "" && $permission != "R"){
				$sSQL = "INSERT INTO data_area_permission " .
						"(data_area, id_profile, permission)" .
						"VALUES" .
						"('".addslashes(sxGPC($dataArea))."',$idProf,'$permission')";
				$objSDAO->exec($sSQL);
			}
		}
	}
	//user permission reload
	$_SESSION[USEROBJ]->reLoad();
	exit();
}

/**
 * 
 * data-area docs management
 * 
 */
function insertDoc($dataArea, &$errMsg){
	global $objSDAO;
	$errMsg = "";
	$fileName = "";
	$name = "";
	if($_FILES["document"]["error"] != 0 || $_FILES["document"]["size"] == 0){
		return true;
	}
	//check existence
	$fileName = elabFileName($_FILES["document"]["name"]);
	$name = addslashes(sxGPC(trim($_REQUEST["doc_name"])));
	if($name == ""){
		$name = $fileName;
	}
	$sSQL = "SELECT count(*) AS numDoc FROM data_area_doc 
			WHERE data_area='$dataArea' AND file_name='$fileName'";
	//put doc in doc-folder
	$objSDAO->query($sSQL, "count");
	$rowCount=$objSDAO->fetch("count");
	if($rowCount["numDoc"] > 0){
		$errMsg = "existing document";
		return false;
	}
	$tmpFile = $_FILES["document"]["tmp_name"];
	if(!file_exists(SERVER_FS_DOC)){
		$errMsg .= _t("doc-folder not found")."<br>";
		return false;
	}
	if(!file_exists(SERVER_FS_DOC."/mb_data_area")){
		mkdir(SERVER_FS_DOC."/mb_data_area",0775);
	}
	if(!file_exists(SERVER_FS_DOC."/mb_data_area/$dataArea")){
		mkdir(SERVER_FS_DOC."/mb_data_area/$dataArea",0775);
	}
	if(!copy($tmpFile, SERVER_FS_DOC."/mb_data_area/$dataArea/$fileName")){
		$errMsg .= _t("copy doc failed")."<br>";
		return false;
	}
	
	$sSQL = "INSERT INTO data_area_doc 
			(data_area, file_name, name, mime_type)
			VALUES
			('".addslashes(sxGPC(trim($dataArea)))."', '$fileName', '$name', '".$_FILES["document"]["type"]."')";
	$objSDAO->exec($sSQL);
	return true;
}

/**
 * clean original file-name
 */
 function elabFileName($nomeFile){
 		$elabFileName = preg_replace('/[^a-zA-Z 0-9 . - _]/', "_", str_replace(" ", "_", $nomeFile));
 		return $elabFileName;
 }
 
 /**
  * delete docs
  */
 function deleteDoc($dataArea, &$errMsg){
 	global $objSDAO;
	$errMsg = "";
	$fileName = "";
	$name = "";
	foreach($_REQUEST as $key=>$value){
		if(substr($key,0,5) == "elim_" && strtolower($value) == "on"){
			$fileName = $_REQUEST["fn_$key"];
			if(file_exists(SERVER_FS_DOC."/mb_data_area/$dataArea/$fileName")){
				unlink(SERVER_FS_DOC."/mb_data_area/$dataArea/$fileName");
			}
			$sSQL = "DELETE FROM data_area_doc WHERE data_area='$dataArea' AND file_name='$fileName'";
			$objSDAO->exec($sSQL);
		}
	}
	return true;
 }
